﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class image_detail : Base
{
    public int id = 0;
    public Model.V_ImageUser img = null;
    public int CommentCount = 0;
    BLL.V_ImageUser bll = new BLL.V_ImageUser();

    public string UserImage = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        id = base.QueryInt32("id");
        BindInfo();

        if (Session["user"] == null)
        {
            respond.Visible = false;
        }
        else
        {

            Model.User user= Session["user"] as Model.User;
            if (user != null)
            {
                UserImage = base.GetImage(user.Pic);
            }
            commentlogin.Visible = false;
        }
    }

    public void BindInfo()
    {
        img = bll.GetModelBystrWhere("id=" + id);
        if (img == null)
        {
            Server.Transfer("/error/404.html", false);
        }
        else
        {
            BindOtherImage(img.UserID.Value);
            BindLoveUser();
            //绑定留言
            BindLiuyan();
        }
    }

    public void BindOtherImage(int uid)
    {
        string sql = string.Format(" UserID={0} and ispass=1 and  ID<> {1} ", uid, id);
        List<Model.V_ImageUser> imgs = bll.GetList(10, sql, " LoveNum desc,addtime desc");
        rptOtherImg.DataSource = imgs;
        rptOtherImg.DataBind();
    }

    public void BindLoveUser()
    {
        BLL.V_FavoritesImg b = new BLL.V_FavoritesImg();
        string sql = string.Format(" id={0} and ispass=1 ", id);
        List<Model.V_FavoritesImg> lists = b.GetList(15, sql, " FavoritesTime desc ");
        this.rptUser.DataSource = lists;
        this.rptUser.DataBind();
    }

    public void BindLiuyan()
    {
        base.PageSize = 6;
        BLL.V_CommentImg b = new BLL.V_CommentImg();
        string sql = string.Format(" ParentID=0 and ImgID={0} ", id);
        Model.PageData<Model.V_CommentImg> data = b.GetList(base.PageSize, base.PageIndex, sql, " AddTime asc ");
        rptLiuyan.DataSource = data.DataSoure;
        rptLiuyan.DataBind();
        pgServer.RecordCount = data.Count;
        pgServer.EnableUrlRewriting = true;
        string url = string.Format("/image/{0}.html", id);
        pgServer.UrlRewritePattern = url + "?page={0}";
        pgServer.DataBind();
        CommentCount = data.Count;
    }

    public string GetChildComment(object o)
    {
        int i = base.ParseInt32(o);
        BLL.V_CommentImg b = new BLL.V_CommentImg();
        string sql = string.Format(" ParentID={0} order by addtime asc   ", i);
        List<Model.V_CommentImg> lists = b.GetList(sql);

        StringBuilder sb = new StringBuilder();
        if (lists != null && lists.Count > 0)
        {

            sb.AppendFormat("<ul class='children'>");
            for (int j = 0; j < lists.Count; j++)
            {
                Model.V_CommentImg item = lists[j];

                sb.AppendFormat(" <li class=\"comment\" id=\"comment-78088\">");
                sb.AppendFormat("    <div class=\"comment_body\">");
                sb.AppendFormat("       <div class=\"comment_meta\">");
                sb.AppendFormat("           <div><img class=\"avatar\" width='50' height='50' src=\"{0}\" /></div>", base.GetImage(item.Pic));
                sb.AppendFormat("           <span>{0}</span>", item.UserName);
                sb.AppendFormat("");
                sb.AppendFormat("");
                sb.AppendFormat("");
                sb.AppendFormat("       </div>");
                sb.AppendFormat("       <div class=\"comment_content\">");
                sb.AppendFormat("           <p>{0}</p>", item.Content);
                sb.AppendFormat("           <div class=\"comm_meta_div\">");
                sb.AppendFormat("               <span class=\"commentmetadata\"></span>{0} [{1}]", base.GetFunnyComment2(j + 1), item.AddTime.Value.ToString());
                sb.AppendFormat("           </div>");
                sb.AppendFormat("");
                sb.AppendFormat("");
                sb.AppendFormat("       </div>");
                sb.AppendFormat("   </div>");
                sb.AppendFormat("</li>");
            }

            sb.AppendFormat("</ul>");
        }
        return sb.ToString();
    }
}